package com.leo.javase.thread.itcast;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class CacheDemo {
	//缓存池
	private Map<String,Object> cache = new HashMap<String,Object>();
	
	private ReadWriteLock rwl = new ReentrantReadWriteLock();
	
	/**
	 * 取缓存的方法
	 * @param key
	 * @return
	 */
	public Object get(String key){
		rwl.readLock().lock();
		Object value = null;
		try{
			value = cache.get(key);
			if(null==value){
				rwl.readLock().unlock();
				rwl.writeLock().lock();
				try{
					value = "empty";
				}finally{
					rwl.writeLock().unlock();
				}
			}
			rwl.readLock().lock();
		}finally{
			rwl.readLock().unlock();
		}
		return value;
		
	}
	
	public static void main(String[] args) {
		
	}

}
